package game;


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Shop {
    public static void enterShop(Player player, Scanner scanner) {
        System.out.println("欢迎来到商店！当前金币：" + player.gold);

        // 从注册表获取所有物品和装备
        List<Item> items = new ArrayList<>(ItemRegistry.getAllItems().values());
        List<Equipment> equipments = new ArrayList<>(ItemRegistry.getAllEquipments().values());

        // 展示出售的物品和装备
        int index = 1;
        System.out.println("可购买的物品：");
        for (Item item : items) {
            System.out.println(index++ + ". " + item.getName() + " - 价格：" + item.getPrice() + "金币");
        }

        System.out.println("可购买的装备：");
        for (Equipment equipment : equipments) {
            System.out.println(index++ + ". " + equipment.getName() + " - 价格：" + equipment.getPrice() + "金币");
        }

        System.out.println("请选择你想要购买的物品：");
        int shopChoice = scanner.nextInt();

        // 处理购买
        try {
            if (shopChoice <= items.size()) {
                purchaseItem(player, items.get(shopChoice - 1));
            } else if (shopChoice <= items.size() + equipments.size()) {
                purchaseItem(player, equipments.get(shopChoice - items.size() - 1));
            } else {
                System.out.println("无效的商店选择！");
            }
        } catch (Exception e) {
            System.out.println("发生错误，请输入有效数字。");
        }
    }

    private static void purchaseItem(Player player, Item item) {
        if (player.gold >= item.price) {
            player.gold -= item.price;
            player.addToInventory(item);
            System.out.println("你购买了 " + item.name + "！");
        } else {
            System.out.println("你的金币不足，无法购买" + item.name + "！");
        }
    }

    private static void purchaseItem(Player player, Equipment equipment) {
        if (player.gold >= equipment.price) {
            player.gold -= equipment.price;
            player.equip(equipment);
            System.out.println("你购买并装备了 " + equipment.name + "！");
        } else {
            System.out.println("你的金币不足，无法购买" + equipment.name + "！");
        }
    }
}